package com.phalanxim.util;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@XmlRootElement(name = "map")
@XmlAccessorType(XmlAccessType.NONE)
public class HashMapXmlAdapter
{
  private Map<String, String> map;

  public Map<String, String> getMap()
  {
    return map;
  }

  public void setMap(Map<String, String> map)
  {
    this.map = map;
  }

  @XmlElement(name = "entry")
  public MapEntry[] getMp()
  {
    List<MapEntry> list = new ArrayList<MapEntry>();
    for (Map.Entry<String, String> entry : map.entrySet())
    {
      MapEntry mapEntry = new MapEntry();
      mapEntry.key = entry.getKey();
      mapEntry.value = entry.getValue();
      list.add(mapEntry);
    }
    return list.toArray(new MapEntry[list.size()]);
  }

  public void setMp(MapEntry[] arr)
  {
    map = new HashMap<String, String>();
    for (MapEntry entry : arr)
    {
      map.put(entry.key, entry.value);
    }
  }

  public static class MapEntry
  {
    @XmlAttribute
    public String key;
    @XmlValue
    public String value;
  }
}
